﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace backupmanagement
{
    public class errlog
    {
        /// <summary>
        /// 生成系统错误日志
        /// </summary>
        private errlog() { }
        public static void errorlog(Exception errmsg)
        {

            String ErrMessage = errmsg.ToString();

            String LogName = "皮革人才网备份服务日志-错误日志";
            String Message = ErrMessage;
            String ErrorPath =AppDomain.CurrentDomain.BaseDirectory + "/App_Log/" + DateTime.Now.ToString("yyyy-MM-dd");

            // 创是否创建系统日志
            WriterLog(ErrorPath, LogName, Message, errmsg.Source);

        }

        #region WriterLog(string path, string logName, string log) 添加文本日志
        /// <summary>
        /// 添加文本日志
        /// </summary>
        /// <param name="path">日志文件路径（不包括文件名）</param>
        /// <param name="logName">参数logName:日志文件名 </param>
        /// <param name="log">异常信息</param>
        public static void WriterLog(string path, string logName, string log)
        {
            string page = "";
            WriterLog(path, logName, log, page);
        }
        #endregion

        #region WriterLog(string path, string logName, string log, string page) 创建文本日志
        //参数:日志文件路径（不包括文件名） 
        //参数logName:日志文件名 
        //参数log:异常信息 
        //参数page:发生异常页面 
        public static void WriterLog(string path, string logName, string log, string page)
        {
            System.DateTime tm = DateTime.Now;
            path = path.Replace("/", @"\").Replace(@"\\", @"\");
            string p = path + "\\" + logName + ".txt";
            if (!Directory.Exists(path))//判断路径下目录是否存在  
            {
                Directory.CreateDirectory(path);
                if (!File.Exists(p))
                {
                    using (StreamWriter sw = File.CreateText(p))
                    {
                        sw.WriteLine(log + "----------" + tm + "[" + page + "]");
                    }
                }
                else
                {
                    using (StreamWriter sw = File.AppendText(p))
                    {
                        sw.WriteLine(log + "----------" + tm + "[" + page + "]");
                    }
                }
            }
            else
            {
                if (!File.Exists(p))
                {
                    using (StreamWriter sw = File.CreateText(p))
                    {
                        sw.WriteLine(log + "----------" + tm + "[" + page + "]");
                    }
                }
                else
                {
                    using (StreamWriter sw = File.AppendText(p))
                    {
                        sw.WriteLine(log + "----------" + tm + "[" + page + "]");
                    }
                }
            }
        }
        #endregion
    }
}
